package com.baizhi.util;

import com.baizhi.entity.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by Administrator on 2019/4/8 0008.
 */
public class FileUpload {
    public static void upload(MultipartFile picture, CmfzBanner cmfzBanner, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            String pictureFileName = picture.getOriginalFilename();
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/img");
            String str2 = UUID.randomUUID().toString();
            String str3 = pictureFileName.substring(pictureFileName.lastIndexOf("."));
            String bannerImageUrl ="/img/"+str2 + str3;
            File destFile = new File(str1 + "/" + str2 + str3);
            picture.transferTo(destFile);
            cmfzBanner.setBannerImageUrl(bannerImageUrl);
        } else {
            cmfzBanner.setBannerImageUrl("0000");
        }
    }

    public static void updatePicture(MultipartFile picture, CmfzBanner cmfzBanner, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/");
            String bannerImageUrl = cmfzBanner.getBannerImageUrl();
            File destFile = new File(str1+bannerImageUrl);
            picture.transferTo(destFile);
        }
    }

    public static void uploadAlbumPicture(MultipartFile picture, CmfzAlbum cmfzAlbum, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            String pictureFileName = picture.getOriginalFilename();
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/audioCollection");
            String str2 = UUID.randomUUID().toString();
            String str3 = pictureFileName.substring(pictureFileName.lastIndexOf("."));
            String albumImage ="/audioCollection/"+str2 + str3;
            File destFile = new File(str1 + "/" + str2 + str3);
            picture.transferTo(destFile);
            cmfzAlbum.setAlbumImage(albumImage);
        } else {
            cmfzAlbum.setAlbumImage("0000");
        }
    }

    public static void updateAlbumPicture(MultipartFile picture, CmfzAlbum cmfzAlbum, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/");
            String albumImage = cmfzAlbum.getAlbumImage();
            File destFile = new File(str1+albumImage);
            picture.transferTo(destFile);
        }
    }

    public static void uploadGuguPicture(MultipartFile picture, CmfzGuru cmfzGuru, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            String pictureFileName = picture.getOriginalFilename();
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/img");
            String str2 = UUID.randomUUID().toString();
            String str3 = pictureFileName.substring(pictureFileName.lastIndexOf("."));
            String guruImage ="/img/"+str2 + str3;
            File destFile = new File(str1 + "/" + str2 + str3);
            picture.transferTo(destFile);
            cmfzGuru.setGuruImage(guruImage);
        } else {
            cmfzGuru.setGuruImage("0000");
        }
    }

    public static void updateGuruPicture(MultipartFile picture, CmfzGuru cmfzGuru, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/");
            String guruImage = cmfzGuru.getGuruImage();
            File destFile = new File(str1+guruImage);
            picture.transferTo(destFile);
        }
    }

    public static void uploadArticlePicture(MultipartFile picture, CmfzArticle cmfzArticle, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            String pictureFileName = picture.getOriginalFilename();
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/si");
            String str2 = UUID.randomUUID().toString();
            String str3 = pictureFileName.substring(pictureFileName.lastIndexOf("."));
            String articleImage ="/si/"+str2 + str3;
            File destFile = new File(str1 + "/" + str2 + str3);
            picture.transferTo(destFile);
            cmfzArticle.setArticleImage(articleImage);
        } else {
            cmfzArticle.setArticleImage("0000");
        }
    }

    public static void updateArticlePicture(MultipartFile picture, CmfzArticle cmfzArticle, HttpServletRequest request) throws IOException {
        boolean b = picture.isEmpty();
        if (!b) {
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/");
            String articleImage = cmfzArticle.getArticleImage();
            File destFile = new File(str1+articleImage);
            picture.transferTo(destFile);
        }
    }

    public static void uploadAudio(MultipartFile audio, CmfzAudio cmfzAudio, HttpServletRequest request) throws IOException {
        boolean b = audio.isEmpty();
        if (!b) {
            String audioFileName = audio.getOriginalFilename();
            ServletContext application = request.getServletContext();
            String str1 = application.getRealPath("/audio");
            String str2 = UUID.randomUUID().toString();
            String str3 = audioFileName.substring(audioFileName.lastIndexOf("."));
            String audioUrl ="/audio/"+str2 + str3;
            File destFile = new File(str1 + "/" + str2 + str3);
            audio.transferTo(destFile);
            cmfzAudio.setAudioUrl(audioUrl);
        } else {
            cmfzAudio.setAudioUrl("0000");
        }
    }

}
